﻿using IDeal.Models;
using IDeal.Services;
using Moq;
using Should;
using Xunit;

namespace IDeal.Tests
{
    public class AcquirerServiceTests
    {
        private readonly Mock<IConfigurationAccessor> _configurationAccessor;
        private readonly Configuration _configuration;
        private readonly ICryptoServices _cryptoServices;
        private readonly ICertificateProvider _certificateProvider;
        private readonly IDealServices _service;

        public AcquirerServiceTests()
        {
            _configurationAccessor = new Mock<IConfigurationAccessor>();
            _configurationAccessor.Setup(x => x.GetConfiguration()).Returns(() => _configuration);

            _configuration = new Configuration
            {
                ApiUrl = "https://abnamro-test.ideal-payment.de/ideal/iDeal",
                MerchantId = "000000000", // enter your merchant ID here
                SubId = "0"
            };

            _certificateProvider = new DefaultCertificateProvider(@"C:\iDeal\bondis.p12", @"9@=-/%{1h}c5A8b"); // enter path to .p12
            _cryptoServices = new CryptoServices(_certificateProvider);
            _service = new IDealServices(_configurationAccessor.Object, _cryptoServices, new DefaultTypeActivator());
        }

        [Fact]
        public void GetIssuers()
        {
            var response = _service.GetIssuers();
            response.Directory.Issuers.ShouldNotBeEmpty();
        }

        [Fact]
        public void CreateTransaction()
        {
            var response = _service.CreateTransaction(
                new TransactionRequest
                    {
                        IssuerId          = "0121",
                        Amount            = 5999,
                        MerchantReturnUrl = "http://localhost",
                        PurchaseId        = "1002",
                        Currency          = "EUR",
                        Description       = "Test betaling",
                        EntranceCode      = "287362",
                        Language          = "nl",
                        MerchantId        = _configuration.MerchantId,
                        SubId             = _configuration.SubId
                    });

            response.IssuerAuthenticationUrl.ShouldNotBeEmpty();
            response.TransactionId.ShouldNotBeEmpty();
        }

        [Fact]
        public void RequestStatus()
        {
            var transactionResponse = _service.CreateTransaction(
                new TransactionRequest
                {
                    IssuerId = "0121",
                    Amount = 5999,
                    MerchantReturnUrl = "http://localhost",
                    PurchaseId = "1002",
                    Currency = "EUR",
                    Description = "Test betaling",
                    EntranceCode = "287362",
                    Language = "nl",
                    MerchantId = _configuration.MerchantId,
                    SubId = _configuration.SubId
                });

            var statusResponse = _service.RequestStatus(transactionResponse.TransactionId);

            statusResponse.AcquirerId.ShouldNotBeEmpty();
            statusResponse.ConsumerAccountNumber.ShouldNotBeEmpty();
            statusResponse.Status.ShouldEqual(TransactionStatus.Success);
        }
    }
}
